package fpzhan.plane.program.struct;

import com.alibaba.fastjson.JSONObject;
import fpzhan.plane.program.compose.ComposeContent;
import fpzhan.plane.program.compose.ComposeContext;
import fpzhan.plane.program.constant.ComposeType;
import fpzhan.plane.program.manager.JsonKeyManager;
import fpzhan.plane.program.util.ListUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class CommentStruct extends CodeBlockStruct {

    public CommentStruct(String name) {
        super(name);
    }

    @Override
    public JSONObject struct(ComposeContext composeContext, ComposeContent composeContent, JSONObject json) throws Exception {
        if (composeContent!=null ){
            if( composeContent.isRepeat()){
                if(composeContent.isRepeatFirst()){
                    json.put(JsonKeyManager.getJsonKey().flowSignCommentKey,composeContent.getSignComment());
                    json.put(JsonKeyManager.getJsonKey().repeatFlowSignNameKey,composeContent.getSignName());
                    json.put(JsonKeyManager.getJsonKey().repeatTimeKey,composeContent.getRepeatTime());
                    json.put(JsonKeyManager.getJsonKey().repeatCommentKey,composeContent.getRepeatComment());
                    return json;
                }else {
                    json=null;
                }
            }else{

                if (ListUtils.isNotEmpty(composeContent.getBelongToSignName()))
                    json.put(JsonKeyManager.getJsonKey().flowBelongSignNameKey, composeContent.getBelongToSignName());
            }

        }
        if (json!=null){
            if (ComposeType.BRANCH.equals(composeContext.getComposeType())) {
                int i=0;
                for (String conditionComment: composeContext.getConditionComment()){
                    i++;
                    JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
                    jsonObject.put(JsonKeyManager.getJsonKey().commentKey,conditionComment);
                    jsonObject.put(JsonKeyManager.getJsonKey().flowKey,json.get(JsonKeyManager.getJsonKey().branchKey+i));

                    json.put(JsonKeyManager.getJsonKey().branchKey+i,jsonObject);
                }
                json.put(JsonKeyManager.getJsonKey().commentKey, composeContext.getComment());
            }else{
                json.put(JsonKeyManager.getJsonKey().commentKey, composeContext.getComment());
            }

        }
        return sortKey(json);
    }

    private JSONObject sortKey(JSONObject json){
        if (json!=null){
            Map map = new HashMap();
            for (String key:
                    json.keySet()) {
                map.put(key,json.get(key));
            }

            json.clear();
            List<String> keySort = JsonKeyManager.getJsonKey().getKeySort();
            for (String key :
                    keySort) {
                if (map.get(key)!=null)json.put(key,map.get(key));
                map.remove(key);
            }
            json.putAll(map);

        }
        return json;
    }
}
